//package com.gitee.feizns.quickstart.utils.dingtalk.api;
//
//import com.dingtalk.api.DefaultDingTalkClient;
//import com.dingtalk.api.DingTalkClient;
//import com.dingtalk.api.request.OapiV2DepartmentGetRequest;
//import com.dingtalk.api.request.OapiV2DepartmentListsubRequest;
//import com.dingtalk.api.request.OapiV2DepartmentListsubidRequest;
//import com.dingtalk.api.request.OapiV2UserGetRequest;
//import com.dingtalk.api.response.OapiV2DepartmentGetResponse;
//import com.dingtalk.api.response.OapiV2DepartmentListsubResponse;
//import com.dingtalk.api.response.OapiV2DepartmentListsubidResponse;
//import com.dingtalk.api.response.OapiV2UserGetResponse;
//import com.gitee.feizns.quickstart.utils.dingtalk.oauth.V1OAuth2;
//import com.taobao.api.ApiException;
//import lombok.RequiredArgsConstructor;
//import lombok.SneakyThrows;
//
//import java.util.List;
//
///**
// * 企业通讯录相关接口
// * 服务端API > 通讯录管理
// * @author feizns
// * @since 2023/9/12
// */
//@RequiredArgsConstructor
//public class ContactsUtils {
//
//    /**
//     * {@link V1OAuth2}
//     */
//    private final V1OAuth2 v1OAuth2;
//
//    /**
//     * 用户接口
//     */
//    private final UserApi userApi = new UserApi();
//
//    /**
//     * 部门接口
//     */
//    private final DepartmentApi departmentApi = new DepartmentApi();
//
//    /**
//     * 服务端API > 通讯录管理 > 用户管理
//     * 用户操作api
//     * @return {@link UserApi}
//     */
//    public UserApi user() {
//        return userApi;
//    }
//
//    /**
//     * 服务端API > 通讯录管理 > 部门管理
//     *
//     * @return {@link DepartmentApi}
//     */
//    public DepartmentApi department() {
//        return departmentApi;
//    }
//
//    /**
//     * 服务端API > 通讯录管理 > 用户管理
//     * @link <a href="https://open.dingtalk.com/document/orgapp/query-user-details">用户详情</a>
//     * @author feizns
//     * @since 2023/09/13
//     */
//    public class UserApi {
//
//        /**
//         * 服务端API > 通讯录管理 > 用户管理查询  > 用户详情
//         * 查询用户详情
//         * @link <a href="https://open.dingtalk.com/document/orgapp/query-user-details">查询用户详情</a>
//         * @param userid {@link String}
//         * @return 返回用户详情
//         */
//        @SneakyThrows
//        public OapiV2UserGetResponse.UserGetResponse get(String userid) {
//            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
//            OapiV2UserGetRequest req = new OapiV2UserGetRequest();
//            req.setUserid(userid);
//            req.setLanguage(v1OAuth2.getConfig().getLanguage());
//            OapiV2UserGetResponse rsp = client.execute(req, v1OAuth2.accessToken().get());
//            if ( !rsp.isSuccess() )
//                throw new ApiException("错误码: " + rsp.getErrorCode() + " 错误消息: " + rsp.getErrmsg(), rsp.getBody());
//            return rsp.getResult();
//        }
//
//    }
//
//    /**
//     * 服务端API > 通讯录管理 > 部门管理
//     * 顶级部门是编号是1
//     * @link <a href="https://open.dingtalk.com/document/orgapp/query-user-details">部门管理</a>
//     * @author feizns
//     * @since 2023/09/13
//     */
//    public class DepartmentApi {
//
//        /**
//         * 服务端API > 通讯录管理 > 部门管理 > 获取部门详情
//         * @link <a href="https://open.dingtalk.com/document/orgapp/query-department-details0-v2">获取部门详情</a>
//         * @param deptId dept编号
//         * @return {@link OapiV2DepartmentGetResponse.DeptGetResponse}
//         */
//        @SneakyThrows
//        public OapiV2DepartmentGetResponse.DeptGetResponse get(Long deptId) {
//            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/get");
//            OapiV2DepartmentGetRequest req = new OapiV2DepartmentGetRequest();
//            req.setDeptId(deptId);
//            req.setLanguage(v1OAuth2.getConfig().getLanguage());
//            OapiV2DepartmentGetResponse rsp = client.execute(req, v1OAuth2.accessToken().get());
//            if ( !rsp.isSuccess() )
//                throw new RuntimeException("错误码: " + rsp.getErrorCode() + " 错误消息: " + rsp.getErrmsg());
//            return rsp.getResult();
//        }
//
//        /**
//         * 服务端API > 通讯录管理 > 部门管理 > 获取子部门ID列表
//         * @link <a href="https://open.dingtalk.com/document/orgapp/query-department-details0-v2">获取子部门ID列表</a>
//         * @param deptId dept编号
//         * @return {@link OapiV2DepartmentListsubidResponse.DeptListSubIdResponse}
//         */
//        @SneakyThrows
//        public List<Long> listSubId(Long deptId) {
//            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsubid");
//            OapiV2DepartmentListsubidRequest req = new OapiV2DepartmentListsubidRequest();
//            req.setDeptId(deptId);
//            OapiV2DepartmentListsubidResponse rsp = client.execute(req, v1OAuth2.accessToken().get());
//            if ( !rsp.isSuccess() )
//                throw new RuntimeException("错误码: " + rsp.getErrorCode() + " 错误消息: " + rsp.getErrmsg());
//            return rsp.getResult().getDeptIdList();
//        }
//
//        /**
//         * 服务端API > 通讯录管理 > 部门管理 > 获取部门列表
//         * @link <a href="https://open.dingtalk.com/document/orgapp/obtain-the-department-list-v2">获取部门列表</a>
//         * @param deptId deptId编号
//         * @return {@link OapiV2DepartmentListsubidResponse.DeptListSubIdResponse}
//         */
//        @SneakyThrows
//        public List<OapiV2DepartmentListsubResponse.DeptBaseResponse> listSub(Long deptId) {
//            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
//            OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest();
//            req.setDeptId(deptId);
//            req.setLanguage(v1OAuth2.getConfig().getLanguage());
//            OapiV2DepartmentListsubResponse rsp = client.execute(req, v1OAuth2.accessToken().get());
//            if ( !rsp.isSuccess() )
//                throw new RuntimeException("错误码: " + rsp.getErrorCode() + " 错误消息: " + rsp.getErrmsg());
//            return rsp.getResult();
//        }
//
//    }
//
//}
